<head>
<meta http-equiv = "content-Type" content = "text/html" charset = "utf-8">
</head>
<?php

$connect = mysql_connect("127.0.0.1", "root", "apmsetup"); //DB가 있는 주소(이것은 웹서버로 직접 접속하는 것이기 때문에 루프백 주소를 써도 됨)
mysql_selectdb("lhu114"); //DB 선택
mysql_query("set names utf8"); //이것 또한 한글(utf8)을 지원하기 위한 것
mysql_query("set names euckr"); //이것 또한 한글(utf8)을 지원하기 위한 것
mysql_query("set session character_set_connection=utf8;");
mysql_query("set session character_set_results=utf8;");
mysql_query("set session character_set_client=utf8;");

$apiKey = "AIzaSyC0xRoYyS_jR3uQ25h3_Sx4-WNfoWF8aW8";

$ParentPhoneNumber = $_REQUEST[ParentPhoneNumber];
$FirstChildSchool = $_REQUEST[FirstChildSchool];
$FirstChildGrade = $_REQUEST[FirstChildGrade];
$FirstChildClass = $_REQUEST[FirstChildClass];
$FirstChildName = $_REQUEST[FirstChildName];
 
$qry = "insert into confirm_request values('$ParentPhoneNumber', '$FirstChildSchool', '$FirstChildGrade', '$FirstChildClass','$FirstChildName');";

$result = mysql_query($qry);

if(!$result)
    echo "fail";
else
	echo "success";

	$qry2 = "select s.ParentName from confirm_request c, schoolinfo s where c.ParentPhoneNumber = s.ParentPhoneNumber and c.FirstChildName = s.FirstChildName;";

	$result2 = mysql_query($qry2);

	if($result2) 
		echo "success";
	else
        echo "fail";

	$row_count=mysql_num_rows($result2); // 레코드 개수

	if($row_count > 0) {

        sendNotificationToPhone($apiKey,$ParentPhoneNumber);

        function sendNotificationToPhone($apiKey,$ParentPhoneNumber) {   

			$headers = array('Content-Type:application/json; charset=UTF-8', 'Authorization:key=AIzaSyC0xRoYyS_jR3uQ25h3_Sx4-WNfoWF8aW8');

		   $connect = mysql_connect("127.0.0.1","root","apmsetup") or die("SQL server에 연결할 수 없습니다."); 
			mysql_selectdb("lhu114") or die("Unable to select database : " . mysql_error()); //DB 선택


		$result3 = mysql_query("SELECT reg_id FROM gcm_table where ParentPhoneNumber = '$ParentPhoneNumber';");
	
		$conidx=0;


		$arr   = array();
		$arr['data'] = array();
		$arr['data']['msg'] = '신분이 확인되었습니다.';
		$arr['registration_ids'] = array();

		while($row = mysql_fetch_array($result3)) {
			$arr['registration_ids'][$conidx] =  $row['reg_id'] ;
		    $conidx++;
		}
		mysql_close($connect);

		$ch = curl_init();

		curl_setopt($ch, CURLOPT_URL,    'https://android.googleapis.com/gcm/send');
		curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
		curl_setopt($ch, CURLOPT_POST,    true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($arr));

		$response = curl_exec($ch);

		curl_close($ch);

	}	
}
	
mysql_close($connect);
?>
